package com.framework.workflow.controller;

import com.framework.common.domain.ResultData;
import com.framework.workflow.params.CompleteParam;
import com.framework.workflow.params.NodeSettingParam;
import com.framework.workflow.params.StartParam;
import com.framework.workflow.service.IPropertyService;
import com.framework.workflow.service.IWfLogService;
import com.framework.workflow.service.IWorkflowService;
import com.framework.workflow.vo.NodeSetting;
import com.framework.workflow.vo.ProcessDetail;
import com.framework.workflow.vo.WfResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wf")
@Api(tags = "流程管理")
public class WorkflowController {
    @Autowired
    private IWorkflowService workflowService;
    @Autowired
    private IPropertyService propertyService;
    @Autowired
    private IWfLogService wfLogService;

    @PostMapping("/start")
    @ApiOperation(value = "启动流程", httpMethod = "POST")
    public ResultData<?> startProcess(@RequestBody StartParam model) {
        WfResult startResult = workflowService.startProcess(model);
        return ResultData.data(startResult);
    }

    @PostMapping("/complete")
    @ApiOperation(value = "审批流程", httpMethod = "POST")
    public ResultData<?> completeTask(@RequestBody CompleteParam model) {
        workflowService.completeTask(model);
        return ResultData.success();
    }

    @PostMapping("/recall")
    @ApiOperation(value="撤销", notes = "撤回到上个节点", httpMethod = "POST")
    public ResultData<?> recallTask(@RequestParam String procInstId) {
        workflowService.recallTask(procInstId);
        return ResultData.success();
    }

    @PostMapping("/delegate")
    @ApiOperation(value="代理", notes = "代理（即：A转给B处理，B处理完，再回到A）", httpMethod = "POST")
    public ResultData<?> delegateTask(@RequestBody CompleteParam param) {
        workflowService.delegateTask(param);
        return ResultData.success();
    }

    @PostMapping(value = "/process")
    @ApiOperation(value = "获取流程审批日志", httpMethod = "POST")
    public ResultData<?> processDiagram( @RequestParam String id) {
        List<ProcessDetail> list = wfLogService.processDiagram(id);
        return ResultData.data(list);
    }

    @PostMapping("/node/attribute")
    @ApiOperation(value = "获取下一个节点配置", httpMethod = "POST")
    public ResultData<?> nextNodeAttribute(@RequestBody NodeSettingParam param) {
        NodeSetting setting =  propertyService.getNodeSetting(param);
        return ResultData.data(setting);
    }
}
